package hx.front.game;

import hx.engine.IRegimentEngine;
import hx.engine.ISectorEngine;
import hx.model.dto.AttackOrder;
import hx.model.dto.House;
import hx.model.dto.Regiment;
import hx.model.dto.Sector;
import hx.model.util.HXCollections;
import hx.model.util.HXKeys;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Action de control de ejércitos.
 * @author kineas
 *
 */
public class ArmyAction extends ActionSupport implements SessionAware,
		ServletRequestAware {

	private static final long serialVersionUID = -6583967004391807404L;
	private Map<String, Object> session;
	private HttpServletRequest servletRequest;
	private IRegimentEngine regimentEngine;
	private ISectorEngine sectorEngine;

	private String sectorId;
	private House house;
	private Set<Sector> sectorList = new HashSet<Sector>();
	private Collection<Regiment> regimentCollection;
	private Collection<Sector> neighbourList = new HashSet<Sector>();
	private Collection<AttackOrder> attackOrderList = new ArrayList<AttackOrder>();

	@Override
	public String execute() throws Exception {
		house = (House) session.get(HXKeys.LOGGED_HOUSE);
		loadCollections();
		return super.execute();
	}

	/**
	 * Carga las colecciones que se utilizarán en la página.
	 */
	private void loadCollections() {
		Collection<Regiment> temporalRegimentList = 
			regimentEngine.findByHouse(house);
		
		for (Regiment regiment : temporalRegimentList) {
			sectorList.add(regiment.getSector());
		}
		
		setAttackOrderList(HXCollections.ATTACK_ORDER_LIST);
		
		if (sectorId != null) {
			Sector sector = sectorEngine.getById(sectorId);
			sector.setId(sectorId);
			regimentCollection = 
				regimentEngine.findByHouseAndSector(house, sector);
			neighbourList = sectorEngine.getNeighbours(sector);
		}
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		servletRequest = arg0;
	}

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	public IRegimentEngine getRegimentEngine() {
		return regimentEngine;
	}

	public void setRegimentEngine(IRegimentEngine regimentEngine) {
		this.regimentEngine = regimentEngine;
	}

	public Set<Sector> getSectorList() {
		return sectorList;
	}

	public void setSectorList(Set<Sector> sectorList) {
		this.sectorList = sectorList;
	}

	public House getHouse() {
		return house;
	}

	public void setHouse(House house) {
		this.house = house;
	}

	public Collection<Regiment> getRegimentCollection() {
		return regimentCollection;
	}

	public void setRegimentCollection(Collection<Regiment> regimentCollection) {
		this.regimentCollection = regimentCollection;
	}

	public Collection<Sector> getNeighbourList() {
		return neighbourList;
	}

	public void setNeighbourList(Collection<Sector> neighbourList) {
		this.neighbourList = neighbourList;
	}

	public Collection<AttackOrder> getAttackOrderList() {
		return attackOrderList;
	}

	public void setAttackOrderList(Collection<AttackOrder> attackOrderList) {
		this.attackOrderList = attackOrderList;
	}

	public ISectorEngine getSectorEngine() {
		return sectorEngine;
	}

	public void setSectorEngine(ISectorEngine sectorEngine) {
		this.sectorEngine = sectorEngine;
	}

	public String getSectorId() {
		return sectorId;
	}

	public void setSectorId(String sectorId) {
		this.sectorId = sectorId;
	}
}
